from __future__ import absolute_import
from .celery import celery
from .utils.video_processor import process_video
import os
from .config import Config


@celery.task(bind=True, name='app.tasks.process_video_task')
def process_video_task(self, input_filename):
    try:
        input_path = os.path.join(Config.UPLOAD_FOLDER, input_filename)
        output_path = os.path.join(Config.PROCESSED_FOLDER, f"processed_{input_filename}")
        
        # 定义进度回调
        def update_progress(p):
            self.update_state(state='PROGRESS', meta={'progress': p})
            
        # 调用处理函数
        process_video(input_path, output_path, progress_callback=update_progress)
        
        return {
            'status': 'SUCCESS',
            'result': os.path.basename(output_path),
            'filepath': os.path.basename(output_path)
        }
    except Exception as e:
        return {
            'status': 'FAILURE',
            'error': str(e),
            'progress': 0
        }
